package com.winter.config;

/**
 * Created by lianghm.ext on 2018/12/10.
 */

import com.winter.task.QuartzTask;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import static com.winter.constant.Constant.GROUP_NAME;


/**
 * 配置任务
 */
@Configuration
public class QuartzConfiguration_02 {

    @Bean(name = "reptilianJob_2")
    public MethodInvokingJobDetailFactoryBean detailFactoryBean(QuartzTask quartzTask) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(false);
        // 设置任务的名字
        jobDetail.setName("reptilianJob_2");
        // 设置任务的分组，在多任务的时候使用
        jobDetail.setGroup(GROUP_NAME);
        // 需要执行的对象
        jobDetail.setTargetObject(quartzTask);
        /*
         * TODO  非常重要
         * 执行QuartzTask类中的需要执行方法
         */
        jobDetail.setTargetMethod("reptilian_2");
        return jobDetail;
    }

    @Bean(name = "jobTrigger_2")
    public CronTriggerFactoryBean cronJobTrigger(JobDetail reptilianJob_2){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(reptilianJob_2);
        //cron表达式，每5 senconds执行一次
        tigger.setCronExpression("0/10 * * * * ?");
        tigger.setName("jobTrigger_2");
        return tigger;
    }

    @Bean(name = "scheduler_2")
    public SchedulerFactoryBean schedulerFactory(Trigger jobTrigger_2){
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        factoryBean.setOverwriteExistingJobs(true);
        // 延时启动，应用启动1秒后
        factoryBean.setStartupDelay(2);
        // 注册触发器
        factoryBean.setTriggers(jobTrigger_2);
        return factoryBean;
    }




}

